package com.liu.cloud.order.controller;

import com.liu.cloud.common.entitys.Payment;
import com.liu.cloud.common.entitys.Result;
import com.sun.xml.internal.bind.v2.util.CollisionCheckStack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class OrderController {


//    public static final String PAYMENT_URL="http://localhost:8001";

    public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";


    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/consumer/pay/create")
    public Result<Payment> create(@RequestBody Payment payment){

        return  restTemplate.postForObject(PAYMENT_URL+"/pay/create",payment,Result.class);
    }

    @GetMapping("/consumer/pay/{id}")
    public Result<Payment> getPayment(@PathVariable("id") long id){
        System.out.printf("***");
        return  restTemplate.getForObject(PAYMENT_URL+"/pay/"+id,Result.class);
    }

    @GetMapping("/order/discovery")
    public Object discovery(){

        List<String> clientServices = discoveryClient.getServices();
        for (String s: clientServices  ) {
            System.out.println("s"+s);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance :instances   ) {
            System.out.println(""+instance.getHost()+"   "+instance.getPort()+"  "+instance.getUri());
        }
       return "hello";
    }

}
